﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class DAOBase
    {
        protected SqlConnection imsConnection;

        public DAOBase()
        {
            string connectionString = ConfigUtilities.GetConnectionString(InventoryConstants.IMSConnectionString,
                                                                          string.Empty);
            imsConnection = new SqlConnection(connectionString);
        }

        protected void OpenConnection()
        {
            try
            {
                if (imsConnection.State == ConnectionState.Closed)
                {
                    imsConnection.Open();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
        }

        protected void OpenConnection(string connnectionStr)
        {
            try
            {
                imsConnection = new SqlConnection(connnectionStr);
                if (imsConnection.State == ConnectionState.Closed)
                {
                    imsConnection.Open();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
        }

        protected void CloseConnection()
        {
            try
            {
                imsConnection.Close();
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
        }

        protected string GetIDString(List<int> idList)
        {
            if (idList != null && idList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("<Data>");
                foreach (int id in idList)
                {
                    stringBuilder.AppendFormat("<Item><ID>{0}</ID></Item>", id);
                }
                stringBuilder.Append("</Data>");
                return stringBuilder.ToString();
            }
            return "<Data></Data>";
        }

        protected string GetBuitupStructuresString(List<Material> idList)
        {
            if (idList != null && idList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("<Data>");
                foreach (Material material in idList)
                {
                    stringBuilder.AppendFormat("<Item><MaterialID>{0}</MaterialID><Quantity>{1}</Quantity></Item>",
                                               material.MaterialID, material.Quantity);
                }
                stringBuilder.Append("</Data>");
                return stringBuilder.ToString();
            }
            return "<Data></Data>";
        }
        protected string GetMaterialLoafString(List<Material> idList)
        {
            if (idList != null && idList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("<Data>");
                foreach (Material material in idList)
                {
                    stringBuilder.AppendFormat("<Item><MaterialID>{0}</MaterialID><WarehouseID>{1}</WarehouseID><Quantity>{2}</Quantity><PricePerUnit>{3}</PricePerUnit></Item>",
                                               material.MaterialID, material.WarehouseId, material.Quantity, material.PricePerUnit); 
                }
                stringBuilder.Append("</Data>");
                return stringBuilder.ToString();
            }
            return "<Data></Data>";
        }
        protected string GetProductLoafString(List<Product> idList)
        {
            if (idList != null && idList.Count > 0)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.Append("<Data>");
                foreach (Product product in idList)
                {
                    stringBuilder.AppendFormat("<Item><MaterialID>{0}</MaterialID><WarehouseID>{1}</WarehouseID><Quantity>{2}</Quantity><PricePerUnit>{3}</PricePerUnit></Item>",
                                               product.ProductID, product.WarehouseID, product.Quantity, product.PricePerUnit);
                }
                stringBuilder.Append("</Data>");
                return stringBuilder.ToString();
            }
            return "<Data></Data>";
        }
    }
}